import concurrent.futures
import collections


ExchangeApiRequest = collections.namedtuple(
  'ExchangeApiRequest', ['market_type', 'exchange', 'api_version'])

AccountRequest = collections.namedtuple(
  'AccountRequest', ['market_type', 'exchange', 'owner'])


def multithread_request_executor(func, request_list, param_list=None):
  results = []
  workers = len(request_list) if len(request_list) <= 24 else 24
  if workers > 0:
    with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor:
      if param_list is None:
        future_list = [executor.submit(func, request) for request in request_list]
      else:
        assert len(request_list) == len(param_list)
        future_list = [
            executor.submit(func, request, *param) for request,
            param in zip(request_list, param_list)
        ]
      for future in future_list:
        results.append(future.result())
  return results


def is_accounting_redundant(market_type, exchange, owner):
  ret = False
  if market_type == 'Spot' and exchange == 'Bitflyer':
    ret = True
  if market_type == 'Options' and exchange == 'Deribit':
    ret = True
  return ret


def filter_invalid_request_and_result(request_list, response_list):
  filtered_request = []
  filtered_result = []
  assert len(request_list) == len(response_list)
  for request, result in zip(request_list, response_list):
    if result:
      filtered_request.append(request)
      filtered_result.append(result)
  return (filtered_request, filtered_result)
